<template>
  <div class="goods-detail-area">
    <div class="top-fixed">
      <van-nav-bar
        :title="detailData.name"
        size="18"
        left-text=""
        right-text=""
        left-arrow
        @click-left="onClickLeft"
        @click-right="onClickRight"
      >
      </van-nav-bar>
    </div>
    <div class="focus-goods">
      <van-swipe @change="onChange">
        <van-swipe-item
          v-for="(item, index) in detailData.picUrls"
          :key="index"
        >
          <img :src="item" />
        </van-swipe-item>
        <template #indicator>
          <div class="custom-indicator">{{ current + 1 }}/4</div>
        </template>
      </van-swipe>
    </div>
    <div class="out-contain">
      <div class="pro-intro">
        <div class="goods-intro">
          <div class="pro-lf">
            <span class="pro-tit"
              >&yen;
              <span class="price">{{ detailData.minPrice || "--" }}</span>
              起</span
            >
          </div>
          <div class="pro-rt">
            <span>{{ detailData.businessKey }}</span>
          </div>
        </div>
        <ul class="dot-lis">
          <li>
            <p class="pro-name">{{ detailData.name }}</p>
          </li>
          <li>
            <p class="fx">
              <span class="left-t">试用</span
              ><span class="right-t">{{
                detailData.freeStatus ? "是" : "否"
              }}</span>
            </p>
          </li>
          <li>
            <p class="fx">
              <span class="left-t">类型</span
              ><span class="right-t"
                ><span v-if="detailData.categoryFIdText"
                  >{{ detailData.categoryFIdText }} -&nbsp;</span
                ><span v-if="detailData.categorySIdText"
                  >{{ detailData.categorySIdText }} -&nbsp;</span
                ><span v-if="detailData.categoryTIdText">{{
                  detailData.categoryTIdText
                }}</span></span
              >
            </p>
          </li>
          <li @click="handleSecSort">
            <p class="fx">
              <span class="left-t">型号</span
              ><span class="right-t">
                {{ skuString }}
                <!-- <span v-for="item in detailData.skus" :key="item.id">
                    {{item.modelId}}
                </span> -->
              </span>
            </p>
          </li>
        </ul>
      </div>
      <div class="product-intro-lis">
        <div class="normal-box">
          <div class="hd">
            <h2><i></i>产品介绍</h2>
          </div>
          <div class="bd">
            <div
              class="innner-html-content"
              v-html="detailData.description"
            ></div>
          </div>
        </div>
        <div class="normal-box">
          <div class="hd">
            <h2><i></i>产品说明书</h2>
          </div>
          <div class="bd">
            <ul class="dl-lis" v-if="detailData.attachList">
              <li
                v-for="(item, index) in detailData.attachList"
                :key="index"
                @click="openNewWindow($event, item)"
              >
                <div class="dl"><i></i>{{ item.fileName }}</div>
              </li>
            </ul>
          </div>
        </div>
      </div>
    </div>
    <div class="btn-bottom-section">
      <van-divider style="margin: 0; padding: 0" />
      <div class="btn-sec">
        <div class="bn-lf bf">
          <van-badge :content="25">
            <div class="b-box">
              <i></i>
              <p>购物车</p>
            </div>
          </van-badge>
        </div>
        <div class="bn-rt bf" @click="goDetail">
          <button>加入购物车</button>
        </div>
      </div>
    </div>
    <!-- 选择型号 -->
    <van-popup
      v-model="showSecPop"
      round
      closeable
      position="bottom"
      :style="{ height: '50%' }"
    >
      <div class="input-style-form">
        <div class="pop-tit"><i></i><span class="tit">添加到购物车</span></div>
        <div class="pop-content">
          <div class="inner-form-area">
            <div class="form-item">
              <p class="label-ft">选择型号</p>
              <div class="item-sec-taglis">
                <ul>
                  <li>
                    <div class="tag-item">
                      <div class="inner-tag-item">D110011</div>
                    </div>
                  </li>
                  <li>
                    <div class="tag-item">
                      <div class="inner-tag-item">D110071</div>
                    </div>
                  </li>
                  <li>
                    <div class="tag-item tag-item-active">
                      <div class="inner-tag-item">D110011</div>
                      <div class="tangle">
                        <van-icon name="success" color="#fff" size="14" />
                      </div>
                    </div>
                  </li>
                  <li>
                    <div class="tag-item">
                      <div class="inner-tag-item">D110011</div>
                    </div>
                  </li>
                </ul>
              </div>
            </div>
            <div class="base-info-form form-pub">
              <van-form label-width="200">
                <van-field
                  v-model="form.price"
                  name="销售单价"
                  input-align="right"
                  label="销售单价"
                  readonly
                  placeholder=""
                />
                <van-field
                  v-model="sms"
                  input-align="left"
                  center
                  clearable
                  label="数量(库存1000)"
                  placeholder=""
                  readonly
                >
                  <template #button>
                    <div class="step-number">
                      <van-stepper v-model="form.total" />
                    </div>
                  </template>
                </van-field>
              </van-form>
            </div>
          </div>
        </div>
      </div>
      <div class="btn-bottom-section">
        <van-divider style="margin: 0; padding: 0" />
        <div class="btn-sec">
          <div
            class="bn-lf bf spl"
            style="display: flex; justify-content: center; align-items: center"
          >
            <span class="spl-price">￥ 139.90</span>
            <van-badge :content="25">
              <div class="b-box">
                <i></i>
                <p>购物车</p>
              </div>
            </van-badge>
          </div>
          <div class="bn-rt bf" @click="goDetail">
            <button>加入购物车</button>
          </div>
        </div>
      </div>
    </van-popup>
  </div>
</template>
<script>
import { createSpu, getSpuDetail, updateSpu } from "@/api/product/spu";
import { filter } from "minimatch";
export default {
  name: "GoodsDetail",
  data() {
    return {
      current: 0,
      showSecPop: false,
      productId: null,
      detailData: null,
      form: {
        price: "￥139",
        total: "",
      },
    };
  },
  computed: {
    skuString() {
      if (this.detailData && this.detailData.skus) {
        let skus = this.detailData.skus.map((item) => item.modelId);
        return skus.join("·");
      }
    },
  },
  async created() {
    this.productId = this.$route.query.id;
    this.getSpuDetail(this.productId);
  },
  methods: {
    openNewWindow(e, item) {
      const url = item.fileUrl;
      window.open(url, "_blank");
    },
    async getSpuDetail(productId) {
      const { data } = await getSpuDetail(productId);

      let skus = data.skus || [];

      // 型号列表
      const modelIds = skus.reduce((t, e) => {
        if (!t.find((item) => item.modelId === e.modelId)) {
          t.push({
            modelId: e.modelId,
            costPrice: e.costPrice,
          });
        }
        return t;
      }, []);

      // 供应商列表
      const vendors = skus.reduce((t, e) => {
        if (!t.find((item) => item.vendorId === e.vendorId)) {
          t.push({
            vendorId: e.vendorId,
            vendorName: e.vendorName,
          });
        }
        return t;
      }, []);

      this.skus = modelIds.map(({ modelId, costPrice }) => {
        return {
          modelId,
          costPrice,
          vendors: vendors.map((vendor) => {
            const current = skus.find(
              (e) => e.modelId === modelId && e.vendorId === vendor.vendorId
            );
            if (current) return current;

            return {
              modelId,
              vendorId: vendor.vendorId,
              vendorName: vendor.vendorName,
              price: 0,
            };
          }),
        };
      });

      //   this.baseForm = data;
      console.log(777, data);
      this.detailData = data;
    },
    handleSecSort() {
      this.showSecPop = true;
    },
    onChange(index) {
      this.current = index;
    },
    onSearch() {
      this.getList();
    },
    onCancel() {
      this.getList();
    },
    onClickLeft() {
      this.$router.go(-1);
    },
    goDetail(item) {
      this.$router.push({
        name: "ShoppingCart",
      });
    },
    async getList() {},
    onClickRight() {},
  },
};
</script>
<style lang="scss">
.goods-detail-area {
  background-color: #fff;
  padding: 0 0 46px 0;
  .top-fixed {
    position: fixed;
    width: 100%;
    z-index: 555;
  }
  .focus-goods {
    padding: 46px 0 0 0;
    width: 100%;
    height: 375px;
    overflow: hidden;
    img {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
  }
  .custom-indicator {
    position: absolute;
    right: 16px;
    bottom: 10px;
    width: 42px;
    height: 20px;
    background: rgba(0, 0, 0, 0.5047);
    border-radius: 10px;
    text-align: center;
    font-size: 12px;
    color: #ffffff;
    line-height: 20px;
    overflow: hidden;
  }
  .out-contain {
    padding: 16px 16px 50px 16px;
    box-sizing: border-box;
    img {
      width: 100%;
      height: auto;
    }
  }
  .pro-intro {
    .goods-intro {
      display: flex;
      align-items: center;

      .pro-lf {
        flex: 1;
      }
      .pro-tit {
        color: #f25353;
        font-size: 14px;
      }
      .price {
        font-size: 20px;
      }
      .pro-rt {
        color: #666;
        font-size: 12px;
      }
    }
    .dot-lis {
      li {
        margin: 20px 0 0 0;
        font-size: 12px;
        .pro-name {
          font-size: 14px;
          color: #222;
          font-weight: 700;
        }
        .left-t {
          width: 48px;
          text-align: left;
          color: #666;
        }
        .right-t {
          color: #222;
        }
        .fx {
          display: flex;
          align-items: center;
        }
      }
    }
  }

  .normal-box {
    margin: 20px 0 0 0;
    h2 {
      margin: 0 6 8px 0;
      padding: 0;
      font-size: 14px;
      color: #222;
      display: inline-block;
      vertical-align: middle;
      i {
        display: inline-block;
        vertical-align: middle;
        width: 3px;
        height: 12px;
        background: #3875f6;
        border-radius: 2px 2px 2px 2px;
        margin: 0 4px 0 0;
      }
    }
  }
  .normal-box-f {
    font-size: 12px;
    margin: 20px 0 0 0;
    .st {
      color: #f25353;
      margin: 0 2px 0 0;
    }
    h2 {
      margin: 0 0 8px 0;
      padding: 0;
      font-size: 14px;
    }
  }
  .dl-lis {
    li {
      margin: 0 0 10px 0;
    }
  }
  .dl {
    color: #3875f6;
    font-size: 12px;

    i {
      background: url(../assets/image/goodsDetail/pdf.png) no-repeat;
      background-size: 100%;
      display: inline-block;
      vertical-align: middle;
      width: 18px;
      height: 20px;
      margin: 0 4px 0 0;
    }
  }
  .btn-bottom-section {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    background-color: #fff;
    .van-badge--fixed {
      top: 10px;
    }
    .b-box {
      text-align: center;
      width: 40px;
      i {
        background: url(../assets/image/goodsDetail/buy.png) no-repeat;
        background-size: 100%;
        display: inline-block;
        width: 18px;
        height: 18px;
        margin: 4px 4px 0 0;
      }
    }
    .btn-sec {
      display: flex;
      .bf {
        flex: 1;
        text-align: center;
      }
      button {
        height: 49px;
        background: #3875f6;
        text-align: center;
        color: #fff;
        width: 100%;
        border: none;
        font-size: 16px;
      }
    }
  }
  .btm-area {
    border-top: 1px solid #efefef;
    display: flex;
    height: 49px;
    line-height: 49px;
    background: #ffffff;
    .btn-reset {
      text-align: center;
      flex: 1;
      font-size: 16px;
      color: #3875f6;
      cursor: pointer;
    }
    .btn-confirm {
      text-align: center;
      font-size: 16px;
      color: #fff;
      background: #3875f6;
      flex: 1;
      cursor: pointer;
    }
  }
  .input-style-form {
    padding: 22px 12px;
    .pop-tit {
      i {
        width: 3px;
        height: 15px;
        background: #3875f6;
        border-radius: 2px 2px 2px 2px;
        display: inline-block;
        vertical-align: middle;
        margin: 0 10px 0 0;
      }
      .tit {
        font-weight: 700;
        font-size: 16px;
        color: #222222;
        line-height: 22px;
        text-align: left;
        display: inline-block;
        vertical-align: middle;
      }
    }
    .inner-form-area {
      padding: 16px 0 0 8px;
    }
    .form-item {
      margin: 0 0 20px 0;
      .label-ft {
        font-size: 14px;
        color: #666666;
        line-height: 20px;
        text-align: left;
        margin: 0 0 4px 0;
      }
      .label-input {
        width: 327px;
        height: 44px;
        background: #ffffff;
        border-radius: 4px 4px 4px 4px;
        border: 1px solid #efefef;
        font-size: 14px;
        padding: 0 6px;
        overflow: hidden;
      }
      .date-picker {
        display: inline-block;
        position: relative;
        .label-input {
          width: 150px;
          height: 44px;
          background: #ffffff;
          border-radius: 4px 4px 4px 4px;
          border: 1px solid #efefef;
        }
        .icon-date {
          position: absolute;
          right: 18px;
          top: 14px;
          display: inline-block;
          img {
            width: 14px;
            height: auto;
          }
        }
      }
    }
  }
  .item-sec-taglis {
    li {
      float: left;
      margin: 0 10px 10px 0;
    }
    .inner-tag-item {
      width: 101px;
      height: 38px;
      background: #f6f6f6;
      font-size: 14px;
      color: #222222;
      line-height: 38px;
      text-align: center;
      border-radius: 4px 4px 4px 4px;
      border: 1px solid #efefef;
      overflow: hidden;
    }
    .tag-item-active {
      display: inline-block;
      position: relative;
      .inner-tag-item {
        width: 101px;
        height: 38px;
        background: #f7faff;
        border-radius: 4px 4px 4px 4px;
        border: 1px solid #3875f6;
        color: #3875f6;
      }
      .tangle {
        width: 18px;
        height: 16px;
        background: #3875f6;
        border-radius: 8px 0px 4px 0px;
        border: 1px solid #3875f6;
        position: absolute;
        right: 0px;
        bottom: 0px;
        text-align: center;
        .van-icon-success {
          margin: 2px 0 0 0;
        }
      }
    }
  }
  .van-field__control--right {
    color: #f25353;
    font-size: 14px;
  }
  .van-cell {
    padding-left: 0;
  }
  .spl {
    .spl-price {
      font-size: 12px;
      color: #f25353;
      margin: 0 20px 0 0;
    }
  }
}
</style>
